package day8.homework;
//通过多线程模拟两人爬山 每个线程表示一个人
//可以设置爬山速度  每爬完100米显示信息
//先到终点的是胜利者 如果有人已经到达重点了 另一个人则停止爬山
public class ClimbMountain{
    int mountainHeight = 8000;
    boolean win = false;
    public ClimbMountain(int mountainHeight){
        this.mountainHeight = mountainHeight;
    }
}
class ClimbThread extends Thread{
    ClimbMountain cm ;
    int speed;
    int sum;
    public ClimbThread(ClimbMountain cm,int speed,String name){
        super(name);
        this.cm = cm;
        this.speed = speed;
    }
    public void run() {
        String name = Thread.currentThread().getName();
        while (true){
            try {
                sleep(speed);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            sum+=100;
            System.out.println(name+"已经跑完了"+sum+"米,剩余"+(cm.mountainHeight-sum)+"米");
            if (sum>=cm.mountainHeight){
                System.out.println(name+"已经到达终点!");
                cm.win = true;
            }
            if (cm.win) break;
        }
    }
    public static void main(String[] args) {
        ClimbMountain cm = new ClimbMountain(800);
        ClimbThread a = new ClimbThread(cm,300,"A");
        ClimbThread b = new ClimbThread(cm,400,"B");
        ClimbThread c = new ClimbThread(cm,500,"C");
        a.start();
        b.start();
        c.start();
    }
}